/*=+--+=#=+--     Unmanned Aerial System Management Software      --+=#=+--+=#*\
|          Copyright (C) 2011 Regents of the University of Colorado.           |
|                             All Rights Reserved.                             |

     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License version 2 as
     published by the Free Software Foundation.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

            Jack Elston                       Cory Dixon                        
|           elstonj@colorado.edu              dixonc@colorado.edu              |
|                                                                              |
\*=+--+=#=+--                 --+=#=+--+=#=+--                    --+=#=+--+=#*/

/***********************************************************************
 *
 * FILENAME:
 * fileServer.cxx
 *
 * PURPOSE:
 *  This code implements the fileServer data structure between tasks on
 *  the flight computer and from ground control station
 *
 * CREATED:
 * 11/2000 by Cory Dixon
 *
 * LAST MODIFIED:
 * $Author: dixonc $
 * $Date: 2005/07/13 21:00:06 $
 * $Revision: 1.2 $
 *
 ***********************************************************************/

#include "VirtualCockpit.h"

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

#include "fileServer.h"
#include "commTask.h"


/***********************************************************************
 *
 ***********************************************************************/
FileServer::FileServer() :
	pThread("FileServer"), log(NULL, FileLog::READ) 
{

}

bool FileServer::init( ShmStruct * shmStructPtr, char *name)
{

	pThread::init(shmStructPtr);

	fileName = string(name);

	if( ! log.open(fileName.c_str()) ) {
		cout << "Could not open " << fileName << endl;
		return false;
	}

	if( (fifo = open(COM_FIFO_NAME, O_WRONLY | O_NONBLOCK)) <= 0) {
		perror("FileServer::FileServer - could not open named fifo");
		return false;
	}

	// after first read, can get time struct tm
	bytesRead = log.read( line );
	tmTime = log.getTime();

	cout << "File: " << fileName << " - " << log.fileSize() << " [bytes]" << endl
		<< "-------------------------" << endl
		<< " year=" << tmTime.tm_year + 1900
		<< " month=" << tmTime.tm_mon + 1
		<< " day=" << tmTime.tm_mday
		<< " hour=" << tmTime.tm_hour
		<< " min=" << tmTime.tm_min
		<< " sec=" << tmTime.tm_sec << endl;

	return true;
}


typedef struct {
	int from;
	int to;
	uint8_t bytes[256];
} TempStr; 

//----------------------------------------------------------
// Update values
//----------------------------------------------------------
void FileServer::update()
{
	int numBytes;

	float lastTOD;
	float delta;
	int n;

	TempStr tempStr;
	tempStr.from = line.from;
	tempStr.to = line.to;
	for(int i=0; i<line.numOfBytes; i++)
		tempStr.bytes[i] = line.bytes[i];

	lastTOD = line.timeOfDay;	

	// send current bytes to fifo
	if( (n = write(fifo, (char *)&tempStr, line.numOfBytes+8)) != line.numOfBytes+8 )
		perror("write: could not send command to fifo");

	// update line
	numBytes = log.read( line );

	if(numBytes <= 0 ) {
		cout << "Reached end of log file" << endl;
		pThread::shutdown();
		return;
	}

	// calc delta time
	delta = line.timeOfDay - lastTOD;
	//cout << "\tdelta=" << delta << endl;

	// sleep 
	usleep((unsigned int)(delta * 1000000));	
}
